from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime


class ResourceBase(BaseModel):
    id: str= Field(default=None, min_length=1, max_length=32, description="资源ID")
    name: str = Field(default=None, min_length=1, max_length=100, description="资源名称")
    code: str = Field(default=None, min_length=1, max_length=50, description="资源代码")
    type: str = Field(default=None, description="资源类型: api, menu, button, data")
    path: Optional[str] = Field(None, max_length=255, description="API路径或菜单路径")
    method: Optional[str] = Field(None, max_length=10, description="HTTP方法")
    description: Optional[str] = Field(None, max_length=255, description="资源描述")
    parent_id: Optional[str] = Field(None, description="父资源ID")
    is_active: bool = Field(True, description="是否激活")
    sort_order: int = Field(0, description="排序")
    tenant_id: str = Field(default=None, max_length=32, description="租户ID")
    created_at: datetime = Field(default_factory=datetime.now, description="创建时间")
    updated_at: datetime = Field(default_factory=datetime.now, description="更新时间")



class ResourceCreate(ResourceBase):
    pass


class ResourceUpdate(BaseModel):
    name: Optional[str] = Field(None, min_length=1, max_length=100)
    code: Optional[str] = Field(None, min_length=1, max_length=50)
    type: Optional[str] = Field(None)
    path: Optional[str] = Field(None, max_length=255)
    method: Optional[str] = Field(None, max_length=10)
    description: Optional[str] = Field(None, max_length=255)
    parent_id: Optional[str] = Field(None)
    sort_order: Optional[int] = Field(None)


class ResourceResponse(ResourceBase):
    id: str
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True
